package dyyx.idle;

import java.time.LocalDateTime;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

public class IdleEchoServerHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println(LocalDateTime.now()+" schannelActive ,remoteAddress=" + ctx.channel().remoteAddress() + ",eventLoop="
				+ ctx.channel().eventLoop());
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) {
		ctx.flush();
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) {
		ctx.write(msg + "\n");
	}

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		if (evt instanceof IdleStateEvent) {
			IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
			System.out.println(LocalDateTime.now()+ " "+ctx.channel().remoteAddress() + " closed ,idleStateEvent=" + idleStateEvent.state().name());
			ctx.channel().close();
		}
	}
}
